package self.zhangkang.chart.view.five

import self.zhangkang.chart.view.IndexInfoHelper
import self.zhangkang.chart.view.IndexNameConfig
import self.zhangkang.chart.view.five.index.FiveAvgTurnVolume
import self.zhangkang.chart.view.five.index.FiveAvgVolume
import self.zhangkang.chart.view.pool.PathPool
import self.zhangkang.chart.view.pool.RectPool

/**
 * @author : Android-张康
 * created on: 2023/8/10 09:48
 * description:  五日分时图指标列表协助类
 */
class FiveAvgIndexInfoHelper(
    private val pathPool: PathPool,
    private val rectPool: RectPool,
    private val avgViewColor: FiveAvgViewColor,
    private val fiveAvgViewDimen: FiveAvgViewDimen,
    private val indexNameConfig: IndexNameConfig
) : IndexInfoHelper<FiveAvgChartIndex, FiveAvgIndexInfo> {

    private val defaultList = listOf(
        FiveAvgChartIndex.FiveAvgChart,
        FiveAvgChartIndex.FiveAvgTurnVolume,
        FiveAvgChartIndex.FiveAvgVolume
    )

    private val mFiveAvgIndexInfo = mutableMapOf<FiveAvgChartIndex, FiveAvgIndexInfo>()

    /**
     * 创建指标
     *
     * @param chartIndex 指标信息
     *
     * @return 返回创建好指标信息
     */
    override fun createKLineIndexInfo(chartIndex: FiveAvgChartIndex): FiveAvgIndexInfo {
        var fiveAvgIndexInfo = mFiveAvgIndexInfo[chartIndex]
        if (null == fiveAvgIndexInfo) {
            fiveAvgIndexInfo = when (chartIndex) {
                FiveAvgChartIndex.FiveAvgChart -> {
                    SimpleFiveAvgChart(
                        pathPool,
                        rectPool,
                        avgViewColor,
                        fiveAvgViewDimen,
                        indexNameConfig
                    )
                }

                FiveAvgChartIndex.FiveAvgTurnVolume -> {
                    FiveAvgTurnVolume(
                        pathPool,
                        rectPool,
                        avgViewColor,
                        fiveAvgViewDimen,
                        indexNameConfig
                    )
                }

                FiveAvgChartIndex.FiveAvgVolume -> {
                    FiveAvgVolume(
                        pathPool,
                        rectPool,
                        avgViewColor,
                        fiveAvgViewDimen,
                        indexNameConfig
                    )
                }
            }
        }
        mFiveAvgIndexInfo[chartIndex] = fiveAvgIndexInfo
        return fiveAvgIndexInfo
    }

    /**
     * 是否可以切换
     *
     * @param currentIndex 当前指标
     *
     * @return true 可以切换下一个，false 不可以切换下一个
     */
    override fun canSwitch(currentIndex: FiveAvgIndexInfo): Boolean {
        return false
    }

    /**
     * 下一个指标
     *
     * @param currentIndex 当前指标
     *
     * @return 返回下一个指标信息
     */
    override fun nextIndex(currentIndex: FiveAvgIndexInfo): FiveAvgIndexInfo {
        var findIndex = -1
        defaultList.forEachIndexed { index, kLineIndex ->
            if (kLineIndex == currentIndex.getIndexInfo()) {
                findIndex = index
                return@forEachIndexed
            }
        }
        if (findIndex == -1) {
            return currentIndex
        }
        if (findIndex >= defaultList.size - 1) {
            return createKLineIndexInfo(defaultList[0])
        }
        return createKLineIndexInfo(defaultList[findIndex + 1])
    }

}